package com.faner.fast.upms.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.faner.fast.base.PageDTO;
import com.faner.fast.constant.CacheConstants;
import com.faner.fast.constant.CommonConstants;
import com.faner.fast.upms.model.SysDictDO;
import com.faner.fast.upms.model.SysRoleDO;
import com.faner.fast.upms.mapper.SysRoleMapper;
import com.faner.fast.upms.service.SysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 系统角色表 服务实现类
 * </p>
 *
 * @author faner
 * @since 2021-01-22
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRoleDO> implements SysRoleService {

    /**
     * 分页查询角色信息
     * @param page 分页对象
     * @return 分页对象
     */
    @Override
    public Page<SysRoleDO> getSysRolePage(PageDTO page) {
        return this.baseMapper.selectPage(new Page<>(page.getPageNum(), page.getPageSize()), null);
    }

    /**
     * 通过用户ID，查询角色信息
     * @param userId
     * @return
     */
    @Override
    public List findRolesByUserId(Long userId) {
        return baseMapper.listRolesByUserId(userId);
    }

    /**
     * 通过角色ID，删除角色,并清空角色菜单缓存
     * @param id
     * @return
     */
    @Override
    @CacheEvict(value = CacheConstants.MENU_DETAILS, allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeRoleById(Long id) {
//        sysRoleMenuMapper.delete(Wrappers.<SysRoleMenu>update().lambda().eq(SysRoleMenu::getRoleId, id));
        return this.removeById(id);
    }
}
